package day05;

/**
 * @author wangzhisen
 * @Desc TODO
 * @date 2025/4/14
 *
 * 练习3：把数组中的数据按照指定格式拼接成一个字符串（模仿Arrays中的toString()）
 */
public class StringDemo3 {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = {};
        int[] arr3 = null;
        System.out.println(arraysToString(arr1));
        System.out.println(arraysToString(arr2));
        System.out.println(arraysToString(arr3));

    }

    public static String arraysToString(int[] arr) {
        if (arr == null ) {
            return "null";
        }
        if (arr.length == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            if (i != 0) {
                sb.append(",");
            }
            sb.append(arr[i]);
        }
        sb.append("]");
        return sb.toString();
    }
}
